/**
 *
 */
package com.sunny.hp.common.servlet;

import java.util.LinkedHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;
import org.springframework.web.servlet.view.RedirectView;

import com.sunny.hp.common.util.LogUtil;
/**
 * LoginInterceptor
 * @author P045701
 *
 */
public class LoginInterceptor implements HandlerInterceptor {
    private Logger logger = LogUtil.getLogger(getClass());
    private static final String MOBILE_VIEWER_VIEW_NAME = "m/";
	/**
	 * 로그인 체크
	 * @param request
	 * @param response 
	 * @return boolean
	 * 
	 * @throws Exception
	 */
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
		String loginPage = "/lg/selectLoginForm.do";
		String servletPath = request.getServletPath();
		String [] pathArray = servletPath.split("/");
		HttpSession session = request.getSession();
        LinkedHashMap userSessionMap = (LinkedHashMap) session.getAttribute("userSession");
        if (userSessionMap == null) {
        	session.setAttribute("request_url", request.getRequestURL());
            throw new ModelAndViewDefiningException(new ModelAndNCOMZView(request, new RedirectView(loginPage)));
        } else {
            return true;
        }
	}

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView mav) throws Exception {
	}
}
